<?php
namespace app\agent\logic;

use app\common\model\UserTginfoModel;

class UserTginfoLogic extends UserTginfoModel
{
	
	public function getPage($where = [], $request = [])
	{
	    $field = [
	        'tginfo.*',
            'user.telephone',
        ];

	    $join = [
	        ['user', 'user.uid = tginfo.uid'],
        ];

		//排序
		$order = [
				'status' => 'DESC',
				$this->getPk() => 'DESC',
		];

		//查询数据
		$data = $this->alias('tginfo')->field($field)->join($join)->where($where)->order($order)->paginate(10, false, ['query' => $request])->each(function ($item)
        {
            //获取商家数量
            $where = [
                ['bind_uid', '=', $item['uid']],
            ];

            $item['seller_count'] = db('wk_seller')->where($where)->count(1);
        });

		//字段属性映射
		foreach ($data as $key => &$val)
		{
			$this->attributeMap($val);
		}
		
		return $data;
	}

	public function updateRecord($data = [])
	{
		//入库
		$data = $this->allowField(true)->save($data, [$this->getPk() => $data['id']]);
		
		if( $data === false )
		{
			return false;
		}
		
		return true;
	}
	
	public function destroyRecord($id = 0)
	{
		$where[] = ['id', '=', $id];

		return $this->where($where)->delete();
	}

	public function getDetail($where = [])
	{
		//查询数据
		$data = $this->where($where)->find();

		//字段属性映射
		$data = $this->attributeMap($data);

		return $data;
	}

    public function dayTginfo($where)
    {
        $join = [
            ['user', 'user.uid = tginfo.uid'],
        ];

        $where[] = ['oktime', '>', strtotime(date('Y-m-d', time()))];

        return $this->alias('tginfo')->join($join)->where($where)->count(1);
	}
}